package com.baizhi.zjs.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.entity.BookEntity;
import com.baizhi.zjs.entity.ShopingCat;



public class ShopingCAtSerivceImpl implements ShopingCAtService{

	@Override
	public void addShop(String id) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		//添加购物车，购物车的信息存放在session中，存放的形式map集合
				//key:图书的id
				//value购物车项
				//创建map集合
				//Map<String, ShopingCat> map=new HashMap<>();
				BookService impl = new BookServiceImpl();
				//获取到需要添加的数据的详细信息
				BookEntity entity = impl.shoeOneBook(id);
				
				//获取购物车
				Map<String, ShopingCat> map =(Map<String, ShopingCat>)session.getAttribute("shopcat");
				//判断有没有购物车，如果没有创建购物车，创建map集合并将需要存放的信息存入map中，创建购物车session
				//将map存入session中
				if(map==null){//如果map为null证明没有购物车
					//创建一个map集合
					Map<String, ShopingCat> mp=new HashMap<>();
					//将图书的数据添加到购物车项中
					//数量，状态，图书对象，小计
					ShopingCat catentity = new ShopingCat(1,1,entity,null,null);
					catentity.setSubtotal(entity.getDdprice() * catentity.getCount());
					catentity.setSubtotaldd(entity.getDdprice() * catentity.getCount());
					System.out.println(catentity);
					mp.put(entity.getId(), catentity);	
					
					saveTotal(mp);
					/*System.out.println("键值对遍历集合=============");
					
					Set<Map.Entry<String,ShopingCat>> entSet=mp.entrySet();
					
					for (Map.Entry<String, ShopingCat> entry : entSet) {
						System.out.println(entry.getKey());
						System.out.println(entry.getValue());
					}*/
					
					Object attribute = session.getAttribute("totalPrice");
					System.out.println("总价-----------------------");
					System.out.println(attribute);
					session.setAttribute("shopcat", mp);
				}else {//否则就是map中有数据
					//判断map中有没有要添加的购物项
					System.out.println("到有session======");
					System.out.println(entity);
					
					
				if(map.containsKey(entity.getId())){//如果map中有要添加的购物项
					//获取这这个键的值
					ShopingCat shopingCat = map.get(entity.getId());
					
					//判断商品从购物车中删除了
					if(shopingCat.getState()==0){
						//1，将状态修改回来
						shopingCat.setState(1);
						/*//2.将数量加1
						shopingCat.setCount(shopingCat.getCount()+1);
						//修改小计信息
						shopingCat.setSubtotal(shopingCat.getYuan());
						shopingCat.setSubtotaldd(shopingCat.getDd());
						session.setAttribute("shopcat", map);
						saveTotal(map);*/
					}
					
					
					//将数量加一
					shopingCat.setCount(shopingCat.getCount()+1);
					//修改小计信息
					shopingCat.setSubtotal(shopingCat.getYuan());
					shopingCat.setSubtotaldd(shopingCat.getDd());
					
					session.setAttribute("shopcat", map);
					
					saveTotal(map);
					Object attribute = session.getAttribute("totalPrice");
					System.out.println("总价-----------------------");
					System.out.println(attribute);
					
					System.out.println("键值对遍历集合=============");
					Set<Map.Entry<String,ShopingCat>> entSet=map.entrySet();
					for (Map.Entry<String, ShopingCat> entry : entSet) {
						System.out.println(entry.getKey());
						System.out.println(entry.getValue());
						
						
					}
					
				}else{//如果map中有要添加的购物项
					ShopingCat catentity = new ShopingCat(1,1,entity,null,null);
					//修改小计信息
					catentity.setSubtotal(entity.getDdprice() * catentity.getCount());
					catentity.setSubtotaldd(entity.getDdprice() * catentity.getCount());
					map.put(entity.getId(), catentity);	
					
					saveTotal(map);
					
					session.setAttribute("shopcat", map);
					/*Object attribute = session.getAttribute("totalPrice");
					System.out.println("总价-----------------------");*/
					//System.out.println(attribute);
				}
				}
	}
	//删除图书（修改图书的状态）
	@Override
	public void deleteShopBook(String id) {
		// TODO Auto-generated method stub
		//获取session
		HttpSession session = ServletActionContext.getRequest().getSession();
		Map<String, ShopingCat> map =(Map<String, ShopingCat>)session.getAttribute("shopcat");
	//通过id获取map集合中的值
		ShopingCat cat = map.get(id);
		System.out.println(cat);
		//将状态修改成0
		 cat.setState(0);
		 System.out.println(cat);
		 //将修改过后的类放入map集合中
		 map.put(id, cat);
		 //将集合重新放入session中
		 session.setAttribute("shopcat", map);
		//重新计算总价
		 saveTotal(map);
	}
	
	
	//将删除的图书恢复，将图书的状态修改为1
		@Override
		public void recoverBook(String id) {
			//获取session
			HttpSession session = ServletActionContext.getRequest().getSession();
			Map<String, ShopingCat> map =(Map<String, ShopingCat>)session.getAttribute("shopcat");
		//通过id获取map集合中的值
			ShopingCat cat = map.get(id);
			System.out.println(cat);
			//将状态修改成0
			 cat.setState(1);
			 System.out.println(cat);
			 //将修改过后的类放入map集合中
			 map.put(id, cat);
			 //将集合重新放入session中
			 session.setAttribute("shopcat", map);
			//重新计算总价
			 saveTotal(map);
			
		}
	
	
	

	@Override
	public void saveTotal(Map<String,ShopingCat> map) {
		// TODO Auto-generated method stub
		//总价
				double totalPrice=0.0;
				//总结省
				double savePrice=0.0;
				
				//遍历购物车
				for (String key : map.keySet()) {

					//根据key获取value
					ShopingCat item = map.get(key);
					
					//判断状态为1计算价格
					System.out.println("标签===========");
					System.out.println(item.getState());
					if(item.getState()==1){
						//总价=当当价*数量 的累加
						System.out.println("价格================");
						System.out.println(item.getBook().getPrice());
						totalPrice+=item.getBook().getPrice()*item.getCount();
						//总结省=(原价-当当价)*数量
						savePrice+=(item.getBook().getPrice()-item.getBook().getDdprice())*item.getCount();
						System.out.println("节省");
						System.out.println(savePrice);
					}
				}

				//获取session
				HttpSession session = ServletActionContext.getRequest().getSession();
				//存储session
				session.setAttribute("totalPrice", totalPrice);  //总价
				session.setAttribute("savePrice", savePrice);  //总结省	
			}
	
	//修改商品的购买数量
	@Override
	public void updateBook(String id, Integer count) {
		//获取session
		HttpSession session = ServletActionContext.getRequest().getSession();
		Map<String, ShopingCat> map =(Map<String, ShopingCat>)session.getAttribute("shopcat");
	//通过id获取map集合中的值
		ShopingCat cat = map.get(id);
		System.out.println(cat);
		//将数量修改成传回来的数量
		cat.setCount(count);
		 
		 cat.setSubtotal(cat.getYuan());
		 cat.setSubtotaldd(cat.getDd());
		 System.out.println(cat);
		 //将修改过后的类放入map集合中
		 map.put(id, cat);
		 //将集合重新放入session中
		 session.setAttribute("shopcat", map);
		//重新计算总价
		 saveTotal(map);
		
	}
	
	

	
	

	
	}
	
	


		
	









	
	
	
	
	
	
	
		


